#include "Scope.h"

void CScope::registerSymbol( const std::string& strName, int num )
{
	if (this->getSymbolTable()->lookup(strName))
	{
		this->getSymbolTable()->registerSymbol(strName, num);
		return;
	}

	CScopePtr pScope = this->getParentScope();

	while (pScope)
	{
		if (pScope->getSymbolTable()->lookup(strName))
		{
			pScope->getSymbolTable()->registerSymbol(strName, num);
			return;
		}
		else
		{
			pScope = pScope->getParentScope();
		}
	}

	this->getSymbolTable()->registerSymbol(strName, num);
	return;
}

int CScope::getSymbol( const std::string& strName )
{
	if (this->getSymbolTable()->lookup(strName))
	{
		return this->getSymbolTable()->getSymbol(strName);

	}

	CScopePtr pScope = this->getParentScope();

	while (pScope)
	{
		if (pScope->getSymbolTable()->lookup(strName))
		{
			return pScope->getSymbolTable()->getSymbol(strName);
		}
		else
		{
			pScope = pScope->getParentScope();
		}
	}

	return this->getSymbolTable()->getSymbol(strName);
}